package com.ky.manage.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.jpeng.jptabbar.BadgeDismissListener;
import com.jpeng.jptabbar.JPTabBar;
import com.jpeng.jptabbar.OnTabSelectListener;
import com.jpeng.jptabbar.anno.NorIcons;
import com.jpeng.jptabbar.anno.SeleIcons;
import com.jpeng.jptabbar.anno.Titles;
import com.ky.manage.BaseApplication;
import com.ky.manage.R;
import com.ky.manage.adapter.FragmentTabAdapter;
import com.ky.manage.base.BaseActivity;
import com.ky.manage.constant.OverallData;
import com.ky.manage.fragment.HomeFragment;
import com.ky.manage.fragment.MineFragment;
import com.ky.manage.fragment.MsgCenterFragment;
import com.ky.manage.model.LoginUserInfo;
import com.ky.manage.model.MsgCntChangeEvent;
import com.ky.manage.permission.AndPermissionUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * 首页
 */
public class MainActivity  extends BaseActivity implements BadgeDismissListener, OnTabSelectListener {

    @Titles
    private static final int[] mTitles = {R.string.home_tab_title_1, R.string.home_tab_title_2,
            R.string.home_tab_title_3};

    @SeleIcons
    private static final int[] mSeleIcons = {R.mipmap.home_selected,R.mipmap.msg_selected,R.mipmap.mine_selected};

    @NorIcons
    private static final int[] mNormalIcons = {R.mipmap.home_unselected, R.mipmap.msg_unselected,R.mipmap.mine_unselected};

    private List<Fragment> mFragmentList = new ArrayList<>();

    private ViewPager mPager;

    private JPTabBar mTabbar;

    private HomeFragment mHomeFragment;

    private MsgCenterFragment mMsgCenterFragment;

    private MineFragment mMineFragment;
    private View mUnloginView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mTabbar = findView(R.id.tabbar);
        mPager = findView(R.id.view_pager);
        mUnloginView= findView(R.id.un_login_rl);
        mUnloginView.setVisibility(BaseApplication.isValidLogin() ? View.GONE : View.VISIBLE);
        findView(R.id.go_to_login_tv, v -> {
            //立即登录
            goToLogin();
        });

        //https://github.com/peng8350/JPTabBar/blob/master/README_CN.md
        //通过注解声明的不需要执行
//        mTabbar.setTitles(mTitles)
//                .setNormalIcons(mNormalIcons)
//                .setSelectedIcons(mSeleIcons)
//                .generate();
        mHomeFragment = new HomeFragment();
        mMsgCenterFragment = new MsgCenterFragment();
        mMineFragment = new MineFragment();
        mTabbar.setGradientEnable(true);
        mTabbar.setPageAnimateEnable(true);
        mTabbar.setTabListener(this);
        mFragmentList.add(mHomeFragment);
        mFragmentList.add(mMsgCenterFragment);
        mFragmentList.add(mMineFragment);

        mPager.setAdapter(new FragmentTabAdapter(getSupportFragmentManager(), mFragmentList));
        mPager.setOffscreenPageLimit(2);
        mTabbar.setContainer(mPager);
        //设置Badge消失的代理
        mTabbar.setDismissListener(this);
        mTabbar.setTabListener(this);
        if(mTabbar.getMiddleView()!=null)
            mTabbar.getMiddleView().setOnClickListener(v -> {
                //突出控件点击
                Toast.makeText(MainActivity.this,"中间点击",Toast.LENGTH_SHORT).show();;
            });

        if (!BaseApplication.isValidLogin()) {
            //跳转登陆页
            goToLogin();
        }
        AndPermissionUtils.getInstance().requestAllPermissions(this, null);
    }

    @Override
    protected void initData() {

    }

    private void goToLogin() {
        toActivity(new Intent(MainActivity.this, OverallData.sEnableSmsCodeLogin ?
                LoginActivity.class : PwdLoginActivity.class));
    }

    @Override
    public void onDismiss(int position) {
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoginStateChange(LoginUserInfo event) {
        if (null == event) {
            return;
        }
        mUnloginView.setVisibility(event.isLogin ? View.GONE : View.VISIBLE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMsgCntChangeEvent(MsgCntChangeEvent event) {
        if (null == event) {
            return;
        }
        mTabbar.showBadge(1, String.valueOf(OverallData.sMsgCnt));
    }

    @Override
    public void onTabSelect(int index) {
        if (index == 1) {
            mTabbar.showBadge(index, String.valueOf(OverallData.sMsgCnt));
        }
    }

    @Override
    public boolean onInterruptSelect(int index) {
        if(index == 1){
            //如果这里有需要阻止Tab被选中的话,可以return true
            return !BaseApplication.isValidLogin(this, true);
        }
        return false;
    }

    public JPTabBar getTabbar() {
        return mTabbar;
    }


}
